package com.library.www.servlet;

import com.alibaba.fastjson.JSON;
import com.google.gson.JsonObject;
import com.library.www.entity.BeOverdue;
import com.library.www.entity.BorrowBooks;
import com.library.www.entity.Page;
import com.library.www.entity.ReturnTheBook;
import com.library.www.service.BorrowService;
import com.library.www.service.impl.BorrowServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class BorrowServlet extends HttpServlet {
    @Override
    protected void doGet (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置中文编码
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        String opr = req.getParameter("opr");
        BorrowService service=new BorrowServiceImpl();
        PrintWriter out = resp.getWriter();
        if (opr==null){
            opr="insert";
        }
        if (opr.equals("insert")){                  //借书
            //拿到新增的值
            String cardId = req.getParameter("cardId");
            Integer cId=Integer.parseInt(cardId);
            String bookId = req.getParameter("bookId");
            Integer bId=Integer.parseInt(bookId);
            String startTime = req.getParameter("startTime");
            String day = req.getParameter("day");
            Integer d=Integer.parseInt(day);
            String phone = req.getParameter("phone");
            BorrowBooks b=new BorrowBooks(cId,bId,startTime,d,phone);
            boolean borrow = service.findBorrow(b);
            PrintWriter writer = resp.getWriter();
            String s = JSON.toJSONString(borrow);
            writer.print(s);
            writer.flush();
            writer.close();
        }else if (opr.equals("insertSelect")){                  //还书
            String id1 = req.getParameter("cardId");
            Integer id=Integer.parseInt(id1);
            String bookId1 = req.getParameter("bookId");
            Integer bookId=Integer.parseInt(bookId1);
            String endTime = req.getParameter("endTime");
            //还书
            ReturnTheBook book=new ReturnTheBook(id,bookId,endTime);
            //是否有借书
            boolean select = service.findSelect(id, bookId);
            String s=null;
            if (select){
                boolean aReturn = service.findReturn(book);
                s = JSON.toJSONString(aReturn);
            }
            out.print(s);
            out.flush();
            out.close();
        }else if(opr.equals("day")){                    //查询借阅天数并判断是否违约
            //获得查询条件
            String id1 = req.getParameter("id");
            Integer id=Integer.parseInt(id1);
            String bookId1 = req.getParameter("bookId");
            Integer bookId=Integer.parseInt(bookId1);
            //调用service查询天数
            int dateTime = service.findDateTime(id, bookId);
            int day = service.findDay(id, bookId);
            boolean man=false;
            //判断是否违约
            if (dateTime>day){
                man=true;
            }
            String s = JSON.toJSONString(man);
            out.print(s);
            out.flush();
            out.close();
        }else if (opr.equals("CBeOverdue")){                //违约分页
            String page1 = req.getParameter("page");
            Integer page=Integer.parseInt(page1);
            String limit1 = req.getParameter("limit");
            Integer limit=Integer.parseInt(limit1);
            String cardName = req.getParameter("name");
            JsonObject Spage = service.page(cardName,page, limit);
//            System.out.print(Spage);
            out.print(Spage);
            out.flush();
            out.close();
        }else if (opr.equals("Delete")){                    //删除
            String id1 = req.getParameter("id");
            Integer id=Integer.parseInt(id1);
            boolean delete = service.findDelete(id);
            String s = JSON.toJSONString(delete);
            out.print(s);
            out.flush();
            out.close();
        }else if (opr.equals("find")){                      //返回约定天数和借书时间
            String cardId1 = req.getParameter("cardId");
            Integer cardId=Integer.parseInt(cardId1);
            String bookId1 = req.getParameter("bookId");
            Integer bookId=Integer.parseInt(bookId1);
            int day2 = service.findDay(cardId,bookId);
            String starTime = service.findStarTime(cardId, bookId);
            BeOverdue beOverdue=new BeOverdue(starTime,day2);
            String s = JSON.toJSONString(beOverdue);
            PrintWriter writer = resp.getWriter();
            writer.print(s);
            writer.flush();
            writer.close();
        }else if (opr.equals("findInsert")){                //新增
            String cardId1 = req.getParameter("cardId");
            Integer cardId=Integer.parseInt(cardId1);
            String bookId1 = req.getParameter("bookId");
            Integer bookId=Integer.parseInt(bookId1);
            String startTime = req.getParameter("startTime");
            String day1 = req.getParameter("day");
            Integer day=Integer.parseInt(day1);
            String endTime = req.getParameter("endTime");
            String type1 = req.getParameter("type");
            Integer type=Integer.parseInt(type1);
            String money1 = req.getParameter("money");
            Double money=Double.parseDouble(money1);
            BeOverdue overdue=new BeOverdue(cardId,bookId,startTime,day,endTime,type,money);
            boolean insert = service.findInsert(overdue);
            String s = JSON.toJSONString(insert);
            out.print(s);
            out.flush();
            out.close();
        }else if(opr.equals("YouFindRepay")){                   //还书分页
            String page1 = req.getParameter("page");
            Integer page=Integer.parseInt(page1);
            String limit1 = req.getParameter("limit");
            Integer limit=Integer.parseInt(limit1);
            String name=req.getParameter("name");
            JsonObject sob=service.findRepay(name,page,limit);
            out.print(sob);
            out.flush();
            out.close();
        }
    }

    @Override
    protected void doPost (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
